//
//  HSYScanQRCodeViewController.m
//  HSY
//
//  Created by 陈浩 on 2017/4/10.
//  Copyright © 2017年 金开门. All rights reserved.
//

#import "HSYScanQRCodeViewController.h"
#import "HSYQRReaderView.h"

@interface HSYScanQRCodeViewController ()<HSYQRReaderViewDelegate>

@property (weak, nonatomic) IBOutlet HSYQRReaderView *readerView;
@property (weak, nonatomic) IBOutlet UIView *scanBox;
@property (weak, nonatomic) IBOutlet UIImageView *scanLineImageView;

@property (nonatomic, assign) BOOL running;

@end

@implementation HSYScanQRCodeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.readerView.delegate = self;
    
    [self start];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setupNavigationBarBackgroundImage{
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage =[UIImage new];

}

- (void)handleGoBackEvent:(UIButton *)sender{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];
    
//    [self startAnimateIfNeed];
    [self.readerView setRectOfInterest:self.scanBox.frame];
}

- (void)start{
    if (self.running) return;
    self.running = YES;
    [self.readerView start];
    [self startAnimate];
}

- (void)stop{
    if (!self.running) return;
    self.running = NO;
    
    [self.readerView stop];
    [self stopAnimate];
}


-(void)startAnimate{
    [self stopAnimate];
    
    CABasicAnimation * animation = [CABasicAnimation animation];
    animation.keyPath = @"position.y";
    animation.toValue = @(self.scanBox.frame.size.height);
    animation.duration = 2.5;
    animation.repeatCount = HUGE_VALF;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    [self.scanLineImageView.layer addAnimation:animation forKey:@"linePositionAnimation"];
}

-(void)stopAnimate{
    [self.scanLineImageView.layer removeAllAnimations];
}

-(void)startAnimateIfNeed{
    if (!self.running) return;
    
    [self stopAnimate];
    
    CABasicAnimation * animation = [CABasicAnimation animation];
    animation.keyPath = @"position.y";
    animation.toValue = @(self.scanBox.frame.size.height);
    animation.duration = 1.5;
    animation.repeatCount = HUGE_VALF;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [self.scanLineImageView.layer addAnimation:animation forKey:@"linePositionAnimation"];
}


#pragma mark - HSYQRReaderViewDelegate -

- (void) readerView: (HSYQRReaderView*) readerView didReadString: (NSString*) resultString{
    HSYLog(@"%@", resultString);
    [self stop];
    
    if ([resultString containsString:@"hsy.qianbaojiajia.com"]) {
        
        if (self.scanResultBlock) {
            NSRange code = [resultString rangeOfString:@"code="];
            NSString *subStr = [resultString substringFromIndex:code.location+code.length];
            subStr = [subStr substringToIndex:[subStr rangeOfString:@"&sign="].location];
            
            self.scanResultBlock(@{@"code":subStr, @"resultString":resultString});
        }
        
    }else{
        self.scanResultBlock(@{@"resultString":resultString});
    }
    [self handleGoBackEvent:nil];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
